layui.use(['jquery','layer','form','table','laydate','upload'],function() {
    var $ = layui.$, layer = layui.layer, form = layui.form, table = layui.table, laydate = layui.laydate,
        upload = layui.upload;
    //获取当前时间
    var myDate = new Date();
    var myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var myMonth = myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
    var myToday = myDate.getDate(); //获取当前日(1-31)
    var myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
    var myHour = myDate.getHours(); //获取当前小时数(0-23)
    var myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
    var mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
    var newTime = myYear+'-'+myMonth+'-'+myToday+' '+myHour+':'+myMinute+':'+mySecond;
    var users = parent.$('#userId').val();

    //获取优惠券列表 赵 /22/1/14
    $.ajax({
        url: '/user/giveCoupon'
        ,data:{}
        ,dataType: 'json'
        ,success:function(data){
            tableRender(data.data);
        }
    });

    function tableRender(data) {
        table.render({
            elem: '#layuiTbale'
            ,data: data
            ,toolbar: '#toolutil'
            ,page: {
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                ,limits:[10,15,20]
                ,first: true
                ,last: true
            }
            ,limit: 10
            ,cols: [[
                {checkbox: true}
                ,{type: 'numbers',title: '序号',align: 'center'}
                ,{field: 'couponCode',title: '优惠券编号',align: 'center'}
                ,{field: 'couponName',title: '优惠券名称',align: 'center'}
                ,{field: 'couponPrice',title: '优惠券金额',align: 'center'}
                ,{field: 'startDate',title: '开始时间',align: 'center'}
                ,{field: 'endDate',title: '结束时间',align: 'center'}
                ,{field: 'issueQuantity',title: '剩余数量',align: 'center',templet:function(data){
                    return data.issueQuantity - data.used;
                    }}
                ,{field: 'type',title: '优惠券类型',align: 'center',templet:function(data){
                    if(data.type == 2){
                        return '商品券'
                    }else if(data.type == 3){
                        return '视频券';
                    }else{
                        return '数据错误';
                    }
                    }}
            ]]
        });
    }

    //存储userId 赵 22/1/14
    var usersCoupon = [];
    //监听复选框 赵 22/1/14
    table.on('checkbox(layuiTbale)', function(obj){
        //查询元素下标
        Array.prototype.indexOf = function (val) {
            for (var i=0; i<this.length; i++){
                if(this[i] == val)
                    return i;
            }
            return -1;
        }
        //删除元素
        Array.prototype.remove = function(val) {
            var index = this.indexOf(val);
            if (index > -1) {
                this.splice(index, 1);
            }
        };
        if(obj.checked){
            usersCoupon.push(obj.data.couponId);
        }else{
            usersCoupon.remove(obj.data.couponId);
        }
    });


    //确定 赵 22/1/14
    determine = function determine(){
        for(var i=0; i<users.length; i++){
            for(var j=0; j<usersCoupon.length; j++){
                $.ajax({
                    url: '/user/addCcouponUsed'
                    ,data: {
                        'couponId': usersCoupon[j]
                        ,'userId': users[i]
                        ,'getDate': newTime
                        ,'remarks': 2
                    }
                    ,dataType: 'json'
                    ,success:function(data){
                        layer.msg('已向用户发放优惠券');
                    }
                });
            }
        }
    }



    //取消 赵22/1/14
    cancel = function cancel(){
        var layer = parent.layer;
        layer.close(layer.index);
    }

});